#ifndef ROOM_H
#define ROOM_H
#include <QObject>
#include <QString>
#include <QJsonObject>
#include <QJsonDocument>
#include <QIODevice>
#include <QDataStream>
#include <QDebug>
#include <QtEndian>

extern bool mode;

constexpr double envTemperature=26;

class room : public QObject{
    Q_OBJECT
public:
    room(QString _roomNo):QObject(nullptr),
        roomNo(_roomNo),
        isOpen(false),
        isServed(false),
        temperature(26),
        cost(0),
        wind(0),
        s_temperature(26),
        s_wind(0)
    {

    }
    void setIsOpen(bool _isOpen){
        this->isOpen=_isOpen;
    }
    void setIsServed(bool _isServed){
        this->isServed=_isServed;
    }
    void setTemperature(double _temperature){
        this->temperature=_temperature;
    }
    void setCost(double _cost){
        this->cost=_cost;
    }
    void setWind(int _wind){
        this->wind=_wind;
    }
    void setSWind(int _wind){
        this->s_wind=_wind;
    }
    void setSTemperature(double _temperature){
        this->s_temperature=_temperature;
    }
    bool getIsOpen(){
        return this->isOpen;
    }
    bool getIsServed(){
        return this->isServed;
    }
    double getTemperature(){
        return this->temperature;
    }
    double getCost(){
        return this->cost;
    }
    int getWind(){
        return this->wind;
    }
    int getSWind(){
        return this->s_wind;
    }
    double getSTemperature(){
        return this->s_temperature;
    }

private:
    QString roomNo;
    bool isOpen;
    bool isServed;
    double temperature;
    double cost;
    int wind;

    int s_wind;
    double s_temperature;

public slots:
    void encapsulateNotification(){
        QByteArray* block = new QByteArray;
        QByteArray jsonArray;
        QJsonObject jsonObj;
        jsonObj.insert("type",1);
        jsonObj.insert("room",this->roomNo);
        jsonObj.insert("temperature",this->temperature);
        QJsonDocument doc;
        doc.setObject(jsonObj);
        jsonArray=doc.toJson(QJsonDocument::Compact);
        //int size=jsonArray.size();
        //size=qToBigEndian(size);
        //block->append(reinterpret_cast<char*>(&size),sizeof(int));
        block->append(jsonArray);
        emit finishEncapsulation(block);
    }

    void encapsulateRequest(){
        QByteArray* block = new QByteArray;
        QByteArray jsonArray;
        QJsonObject jsonObj;
        jsonObj.insert("type",0);
        jsonObj.insert("room",this->roomNo);
        jsonObj.insert("switch",this->isOpen?1:0);
        jsonObj.insert("temperature",this->s_temperature);
        jsonObj.insert("wind",this->s_wind);
        qDebug()<<"send retuest wind "<<this->s_wind;
        QJsonDocument doc;
        doc.setObject(jsonObj);
        jsonArray=doc.toJson(QJsonDocument::Compact);
        //int size=jsonArrray.size();
        //size=qToBigEndian(size);
        //block->append(reinterpret_cast<char*>(&size),sizeof(int));
        block->append(jsonArray);
        emit finishEncapsulation(block);
    }

public slots:
    void setValue(bool _isOpen,double _temperature,int _wind,double _cost,bool _isServed){
        this->isOpen=_isOpen;
        this->s_temperature=_temperature;
        this->cost=_cost;
        this->isServed=_isServed;
        this->wind=_wind;
        if(this->isOpen&&this->wind>0&&fabs(this->temperature-this->s_temperature)>=0.1){
            if(mode==false&&this->temperature>this->s_temperature){
                switch(this->wind){
                case 1:
                    this->temperature-=0.05;
                    break;
                case 2:
                    this->temperature-=0.1;
                    break;
                case 3:
                    this->temperature-=0.2;
                    break;
                default:
                    break;
                }
            }
            else if(mode==true&&this->temperature<this->s_temperature){
                switch(this->wind){
                case 1:
                    this->temperature+=0.05;
                    break;
                case 2:
                    this->temperature+=0.1;
                    break;
                case 3:
                    this->temperature+=0.2;
                    break;
                default:
                    break;
                }
            }
        }
        else if(this->isOpen&&(this->wind==0)&&(fabs(this->temperature-envTemperature)>=0.1)){
            if(this->temperature>envTemperature){
                this->temperature-=0.05;
            }
            else{
                this->temperature+=0.05;
            }
        }
    }

signals:
    void finishEncapsulation(QByteArray* p_block);
};

#endif // ROOM_H
